package com.youuvs.org.controller;

import com.youuvs.basic.utils.JsonResult;
import com.youuvs.basic.utils.PageList;
import com.youuvs.org.domian.Department;
import com.youuvs.org.query.DepartmentQuery;
import com.youuvs.org.service.IDepartmentService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/department")
public class DepartmentController {
    @Autowired
    private IDepartmentService departmentService;

    @GetMapping
    public List<Department> findAll(){
        List<Department> lists=departmentService.findAll();
        return lists;
    }
    @GetMapping("/{id}")
    public Department findById(@PathVariable("id") Long id){
        return departmentService.findById(id);
    }
    @DeleteMapping("/{id}")
    public JsonResult delete(@PathVariable("id") Long id){
        try {
            departmentService.delete(id);
            return new JsonResult();
        } catch (Exception e) {
            e.printStackTrace();
            return new JsonResult().me();
        }
    }

    @PutMapping
    public JsonResult insertOrUpdate(@RequestBody Department department){
        try {
            if(department.getId()==null){
                departmentService.insert(department);
            }else{
                departmentService.update(department);
            }
            return new JsonResult();
        } catch (Exception e) {
            e.printStackTrace();
            return new JsonResult().me();
        }
    }

    @PostMapping
    public PageList<Department> pageQuery(@RequestBody DepartmentQuery query){
        try {
            PageList<Department> lists=departmentService.queryPage(query);
           return lists;
        } catch (Exception e) {
            e.printStackTrace();
            return new PageList<>();
        }
    }

    @PatchMapping
    //@RequestBody 将参数放进请求体中
    public JsonResult patchDel(@RequestBody List<Long> ids){

        try {
            departmentService.patchDel(ids);
            return new JsonResult();
        } catch (Exception e) {
            e.printStackTrace();
            return new JsonResult().me();
        }

    }
    @GetMapping("/deptTree")
    public List<Department> getTree(){
        return departmentService.getTree();
    }


}
